//
//  ECMemberCommonTableView.m
//  ECERP.CRM
//
//  Created by 刘思麟 on 2018/11/4.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECMemberCommonTableView.h"
#import "ECCustomSectionTapView.h"

@interface ECMemberCommonTableView()<ECCustomSectionTapViewDelegate>

@property (nonatomic,strong) ECCustomSectionTapView * customIndexView;

@end

@implementation ECMemberCommonTableView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.tableView = [[UITableView alloc] init];
        self.tableView.showsVerticalScrollIndicator = NO;
        WS(weakSelf, self);
        [self addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(weakSelf);
        }];
        [self addSubview:self.customIndexView];
        [self.customIndexView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.mas_equalTo(weakSelf);
            make.width.mas_equalTo(30 * PX_RELATIVE375);
        }];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.ecDelegate) {
        if ([self.ecDelegate respondsToSelector:@selector(titlesOfSectionIndex)]) {
            NSArray * arr = [self.ecDelegate performSelector:@selector(titlesOfSectionIndex) withObject:nil];
            self.customIndexView.hidden = !(arr.count > 0);
            self.customIndexView.titles = arr;
            [self.customIndexView setNeedsLayout];
        }
    }
}

#pragma mark -ECCustomSectionTapViewDelegate

- (void)selectedAtIndex:(NSInteger)index {
    if (self.ecDelegate && [self.ecDelegate respondsToSelector:@selector(selectedTitleAtIndex:)]) {
        [self.ecDelegate selectedTitleAtIndex:index];
    }
}

- (ECCustomSectionTapView *)customIndexView {
    if (!_customIndexView) {
        _customIndexView = [ECCustomSectionTapView new];
        _customIndexView.backgroundColor = [UIColor clearColor];
        _customIndexView.hidden = YES;
        _customIndexView.delegate = self;
        _customIndexView.fontSize = 12;
        _customIndexView.charSplit = 1;
    }
    return _customIndexView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.001)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.001)];
        _tableView.backgroundView.backgroundColor = ECCOLOR(0xF4F4F4);
        _tableView.backgroundColor = ECCOLOR(0xF4F4F4);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

@end
